import {readFile} from '../../../src/utils'
import { expect } from 'chai';

import {JavaJUnitParser} from '../../../src/parsers/java-junit/java-junit-parser'
import {Coverage} from '../../../src/test-parser'


describe('count', () => {

    it('should correctly calculate coverage from the HTML content', async () =>{

        const mavenData = await readFile('./test/parsers/java-junit/maven-test.html')
        const expectedCoverage: Coverage = {
            total: 2,
            passed: 1,
            failed: 1,
            skipped: 0,
            passedRate: 50
        };

        const javaParser = new JavaJUnitParser()
        const res = await javaParser.count(mavenData)
        expect(res).to.deep.equal(expectedCoverage)
    })
})

describe('File Path Converters', () => {
    describe('coverageFile', () => {
        it('should throw an error if the path does not end with .html', async () => {
            try {
                const invalidPath = 'path/to/report.json';
                const javaParser = new JavaJUnitParser()
                await javaParser.coverageFile(invalidPath);
                expect.fail('Expected error was not thrown');
            } catch (error) {
                expect(error).to.be.an('error');
                // @ts-ignore
                expect(error.message).to.equal(`Input variable 'reportPath' should reference a report with html extension`);
            }
        });
        it('should return the correct path if it ends with .html and exsits', async () => {
            const validPath = './test/parsers/java-junit/maven-test.html';
            const javaParser = new JavaJUnitParser()
            const result = await javaParser.coverageFile(validPath);
            expect(result).to.equal(validPath);
        });
    });
    describe('htmlFile', () => {
        it ('should return the same path', () => {
            const path = './test/parsers/java-junit/maven-test.html'
            const javaParser = new JavaJUnitParser()
            expect(javaParser.htmlFile(path)).to.equal(path)
        })
    });
});